﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 企业人事管理系统
{
    public partial class DepartChangeForm : Form
    {
        private DataTable dt;

        public DepartChangeForm(string nowDepart)
        {
            InitializeComponent();
            textBox1.Text = nowDepart;
        }

        private void DepartChangeForm_Load(object sender, EventArgs e)
        {
            //获取当前的上级部门
            OledbOP.initConnect(DataName.testConStr);
            OleDbDataReader re = null;
            try
            {
                string sql = string.Format("select {0} from {1} where {2}='{3}'",
                    DataName.departUP,
                    DataName.departRelateTable,
                    DataName.departDown,
                    textBox1.Text.Trim());
                re = OledbOP.getReader(sql);
                re.Read();
                textBox2.Text =  re.GetString(0);
                
            }catch
            {
                textBox2.Text = "无";
            }
            finally
            {
                if(re!=null)
                {
                    re.Close();
                }
            }

            //加载listbox
            load_listBox();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //当上下级都存在时更新数据
            string sqlUpdate = string.Format(
                "update {2} set {3}='{1}' where {4}='{0}'",
                textBox1.Text.Trim(), //0
                textBox3.Text.Trim(), //1
                DataName.departRelateTable, //2
                DataName.departUP, //3
                DataName.departDown);  //4

            //该部门上级部门为“无”时插入上下级关系
            string sqlInsert = string.Format(
                "insert into {2}({3},{4}) values('{1}','{0}')",
                textBox1.Text.Trim(), //0
                textBox3.Text.Trim(), //1
                DataName.departRelateTable, //2
                DataName.departUP, //3
                DataName.departDown);  //4

            //该部门的上级部门改为“无”时删除上下级关系
            string sqlDelete = string.Format(
                "delete from {2} where {4}='{0}'",
                textBox1.Text.Trim(), //0
                textBox3.Text.Trim(), //1
                DataName.departRelateTable, //2
                DataName.departUP, //3
                DataName.departDown);  //4
               

            string sql = null;
            if(textBox3.Text.Trim().Equals("无"))
            {
                sql = sqlDelete;
            }
            else if(textBox2.Text.Trim().Equals("无"))
            {
                sql = sqlInsert;
            }
            else
            {
                sql = sqlUpdate;
            }
            try
            {
                if (OledbOP.execute(sql) > 0)
                {
                    MessageBox.Show("更新成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("更新无效！");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("更新失败！");
                MessageBox.Show(ex.ToString());
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void load_listBox()
        {
            string sqlStr = String.Format("select {0},{1} from {2}",
                DataName.departNumber,
                DataName.departName,
                DataName.departTable);
            dt = OledbOP.getTable(sqlStr);
            listBox1.Items.Clear();
            string text = "0" + "-" + "无";
            listBox1.Items.Add(text);
            for (int i=0;i<dt.Rows.Count;i++)
            {
                text = dt.Rows[i][0] + "-" + dt.Rows[i][1];
                listBox1.Items.Add(text);
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            char[] ch = { '-' };
            if(listBox1.Items.Count>0)
            {
                for(int i=0;i<listBox1.Items.Count;i++)
                {
                    if(listBox1.Items[i].ToString().Contains(textBox4.Text.Trim()))
                    {
                        listBox1.SelectedItem = listBox1.Items[i];
                        string[] text = listBox1.Items[i].ToString().Split(ch);
                        textBox3.Text = text[1];
                        return;
                    }
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            char[] ch = { '-' };
            int i = listBox1.SelectedIndex;
            string[] text = listBox1.Items[i].ToString().Split(ch);
            textBox3.Text = text[1];
        }
    }
}
